package com.yfjin.webrtc

import android.os.Bundle
import android.view.LayoutInflater
import android.view.Surface
import android.view.SurfaceHolder
import android.view.SurfaceView
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import com.yfjin.webrtc.databinding.ActivityMain2Binding
import com.yfjin.webrtc.ws.DecodecPlayerLiveH264
import com.yfjin.webrtc.ws.EncoderPlayerLiveH264
import com.yfjin.webrtc.ws.IPeerConnection
import com.yfjin.webrtc.ws.SocketLive


class MainActivity : AppCompatActivity(), IPeerConnection {

    companion object {
    }

    private lateinit var mBinding: ActivityMain2Binding


    var mSurfaceList: MutableList<Surface> = ArrayList()
    var mEncoderPlayerLiveH264: EncoderPlayerLiveH264? = null
    var mSocketLive: SocketLive? = null
    var mDecoderList: MutableList<DecodecPlayerLiveH264> = ArrayList()

    private var mSurfaceIndex = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        mBinding = DataBindingUtil.inflate(
            LayoutInflater.from(this),
            R.layout.activity_main2,
            null,
            false
        )
        setContentView(mBinding.getRoot())


        val surfaceViews: ArrayList<SurfaceView> = ArrayList()
        surfaceViews.add(mBinding.sv1)
        surfaceViews.add(mBinding.sv2)
        surfaceViews.add(mBinding.sv3)
        for (view in surfaceViews) {
            view.holder.addCallback(object : SurfaceHolder.Callback {
                override fun surfaceCreated(holder: SurfaceHolder) {
                    mSurfaceList.add(holder.surface)
                }

                override fun surfaceChanged(
                    holder: SurfaceHolder,
                    format: Int,
                    width: Int,
                    height: Int
                ) {
                }

                override fun surfaceDestroyed(holder: SurfaceHolder) {}
            })
        }
        mSocketLive = SocketLive(this)



        mBinding.meetingHangupImageView.setOnClickListener {
            connectRoom()
        }
    }


    fun connectRoom() {
        mSocketLive?.start(this)
        mEncoderPlayerLiveH264 = EncoderPlayerLiveH264(this, mBinding.sv0)
        mEncoderPlayerLiveH264?.startCapture(mSocketLive)
    }

    override fun newConnection(remoteIp: String?) {
        val decodecPlayerLiveH264 = DecodecPlayerLiveH264()
        decodecPlayerLiveH264.initDecoder(remoteIp, mSurfaceList[mSurfaceIndex++])
        mDecoderList.add(decodecPlayerLiveH264)
    }

    override fun remoteReceiveData(remoteIp: String?, data: ByteArray?) {
        val decodecPlayerLiveH264: DecodecPlayerLiveH264? = findDecodec(remoteIp!!)
        decodecPlayerLiveH264?.sendData(data)
    }

    private fun findDecodec(remoteIp: String): DecodecPlayerLiveH264? {
        for (decodecPlayerLiveH264 in mDecoderList) {
            if (decodecPlayerLiveH264.remoteIp == remoteIp) {
                return decodecPlayerLiveH264
            }
        }
        return null
    }
}